package com.xueyi.workflow.controller;

import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.workflow.domain.query.RoleProcessPermQuery;
import com.xueyi.workflow.domain.dto.RoleProcessPermDto;
import com.xueyi.workflow.service.RoleProcessPermService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.GetMapping;
import com.xueyi.common.core.web.result.AjaxResult;
import java.util.List;

@Tag(name = "角色-流程权限分配管理")
@RestController
@RequestMapping("/roleProcessPerm")
public class RoleProcessPermController extends BaseController<RoleProcessPermQuery, RoleProcessPermDto, RoleProcessPermService> {
    @Autowired
    private RoleProcessPermService baseService;

    @Override
    protected String getNodeName() {
        return "角色-流程权限分配";
    }

    /**
     * 角色批量分配流程权限
     */
    @PostMapping("/batchAssign")
    public AjaxResult batchAssign(@RequestBody BatchAssignRequest request) {
        return baseService.batchAssign(request.getRoleId(), request.getProcessKeys());
    }

    /**
     * 角色批量解绑流程权限
     */
    @PostMapping("/batchUnbind")
    public AjaxResult batchUnbind(@RequestBody BatchAssignRequest request) {
        return baseService.batchUnbind(request.getRoleId(), request.getProcessKeys());
    }

    /**
     * 校验角色对流程的权限
     */
    @GetMapping("/checkPerm")
    public AjaxResult checkPerm(@RequestParam Long roleId, @RequestParam String processKey) {
        return AjaxResult.success(baseService.checkPerm(roleId, processKey));
    }

    /**
     * 分页查询角色-流程权限分配（GET方式）
     */
    @GetMapping("/list")
    public AjaxResult list(RoleProcessPermQuery query) {
        startPage();
        List<RoleProcessPermDto> list = baseService.selectListScope(query);
        return getDataTable(list);
    }

    /**
     * 批量分配/解绑请求体
     */
    public static class BatchAssignRequest {
        private Long roleId;
        private List<String> processKeys;
        public Long getRoleId() { return roleId; }
        public void setRoleId(Long roleId) { this.roleId = roleId; }
        public List<String> getProcessKeys() { return processKeys; }
        public void setProcessKeys(List<String> processKeys) { this.processKeys = processKeys; }
    }
}
